package views
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Vector3D;
	import flash.media.Sound;
	import flash.media.SoundLoaderContext;
	import flash.net.Socket;
	import flash.net.URLRequest;
	
	import jiglib.collision.CollisionInfo;
	import jiglib.geometry.JSphere;
	import jiglib.physics.RigidBody;
	import jiglib.plugin.papervision3d.Papervision3DPhysics;
	import jiglib.plugin.papervision3d.Pv3dMesh;
	
	import org.papervision3d.core.ns.pv3dview;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
	
	import spark.components.Button;
	import spark.components.Label;
	import spark.components.TextInput;
	
	[SWF(backgroundColor="#888888")]
	public class EngineBilliard extends BasicView
	{
		public var papervision3dphysics:Papervision3DPhysics;
		public var arrRigidBodies:Array = new Array() ;
		public var arrSpheres:Array = new Array();
		public var flagPower:Boolean = false;
		public var buttonShooting:Button;
		public var labeScore:Label;
		public var indexShooting:int = 0;
		
		public var flagOther:Boolean = false;
		public var flagRed1:Boolean = false;
		public var flagRed2:Boolean = false;
		
		
		
		
		public function EngineBilliard(viewportWidth:Number=640, viewportHeight:Number=480, scaleToStage:Boolean=true, interactive:Boolean=false, cameraType:String="Target")
		{
			
			var colormaterialred:ColorMaterial = new ColorMaterial(0xff0000);
			var colormaterialyellow:ColorMaterial = new ColorMaterial(0xffff00);
			var colormaterialwhite:ColorMaterial = new ColorMaterial(0xf0f0f0);
			var colormaterialgreen:ColorMaterial = new ColorMaterial(0x00ff00); 
			
			
			//			var colormaterialred:WireframeMaterial = new WireframeMaterial(0xff0000);
			//			var colormaterialyellow:WireframeMaterial = new WireframeMaterial(0xffff00);
			//			var colormaterialwhite:WireframeMaterial = new WireframeMaterial(0xf0f0f0);
			//			var colormaterialgreen:WireframeMaterial = new WireframeMaterial(0x00ff00);
			var materialslistgreen:MaterialsList = new MaterialsList();
			materialslistgreen.addMaterial(colormaterialgreen,"all");
			
			
			super(viewportWidth, viewportHeight, scaleToStage, interactive, cameraType);
			
			
			camera.x = 0;
			camera.y = 700;
			camera.z = -1500;
			camera.zoom = 100;
			
			camera.lookAt(new DisplayObject3D());
			
			papervision3dphysics = new Papervision3DPhysics(scene);
			
			
			
			var rigidbodywallleft:RigidBody = papervision3dphysics.createCube(materialslistgreen,30,800,30);
			var rigidbodywallright:RigidBody = papervision3dphysics.createCube(materialslistgreen,30,800,30);
			var rigidbodywallfront:RigidBody = papervision3dphysics.createCube(materialslistgreen,600,30,30);
			var rigidbodywallback:RigidBody = papervision3dphysics.createCube(materialslistgreen,600,30,30);
			
			var sphereWhite:Sphere = new Sphere(colormaterialwhite,20);
			var jsphereWhite:JSphere= new JSphere(new Pv3dMesh(sphereWhite),20);
			papervision3dphysics.addBody(jsphereWhite);
			
			var sphereYellow:Sphere = new Sphere(colormaterialyellow,20);
			var jsphereYellow:JSphere= new JSphere(new Pv3dMesh(sphereYellow),20);
			papervision3dphysics.addBody(jsphereYellow);
			
			var sphereRed1:Sphere = new Sphere(colormaterialred,20);
			var jsphereRed1:JSphere= new JSphere(new Pv3dMesh(sphereRed1),20);
			papervision3dphysics.addBody(jsphereRed1);
			
			var sphereRed2:Sphere = new Sphere(colormaterialred,20);
			var jsphereRed2:JSphere= new JSphere(new Pv3dMesh(sphereRed2),20);
			papervision3dphysics.addBody(jsphereRed2);
			
			var rigidbodyGround:RigidBody =
				papervision3dphysics.createGroundMe(new WireframeMaterial(0x111111),1000,-10);
			
			
			arrSpheres.push(sphereWhite);
			arrRigidBodies.push(jsphereWhite);
			
			arrSpheres.push(sphereYellow);
			arrRigidBodies.push(jsphereYellow);
			
			arrSpheres.push(sphereRed1);
			arrRigidBodies.push(jsphereRed1);
			
			arrSpheres.push(sphereRed2);
			arrRigidBodies.push(jsphereRed2);
			
			
			scene.addChild(arrSpheres[0]);
			scene.addChild(arrSpheres[1]);
			scene.addChild(arrSpheres[2]);
			scene.addChild(arrSpheres[3]);
			
			
			arrRigidBodies[0].y = 100;	arrRigidBodies[0].restitution = 1;	arrRigidBodies[0].friction = 0.7;arrRigidBodies[0].x =    0;arrRigidBodies[0].z = -100;
			arrRigidBodies[1].y = 100;	arrRigidBodies[1].restitution = 1;	arrRigidBodies[1].friction = 0.7;arrRigidBodies[1].x =   40;arrRigidBodies[1].z =  200;
			arrRigidBodies[2].y = 100;	arrRigidBodies[2].restitution = 1;	arrRigidBodies[2].friction = 0.7;arrRigidBodies[2].x =    0;arrRigidBodies[2].z =  200;
			arrRigidBodies[3].y = 100;	arrRigidBodies[3].restitution = 1;	arrRigidBodies[3].friction = 0.7;arrRigidBodies[3].x =    0;arrRigidBodies[3].z = -200;
			
			rigidbodywallleft.restitution =0.7;
			rigidbodywallleft.x = -300;
			rigidbodywallleft.movable = false;
			rigidbodywallleft.friction = .7;
			
			rigidbodywallright.restitution =0.7;
			rigidbodywallright.x = 300;
			rigidbodywallright.movable = false;
			rigidbodywallright.friction = .7;
			
			rigidbodywallfront.restitution =0.7;
			rigidbodywallfront.z = 400;
			rigidbodywallfront.movable = false;
			rigidbodywallfront.friction = .7;
			
			rigidbodywallback.restitution =0.7;
			rigidbodywallback.z = -400;
			rigidbodywallback.movable = false;
			rigidbodywallback.friction = .7;
			
			
			
			rigidbodyGround.restitution = 1;
			rigidbodyGround.friction = .8;
			
			startRendering();
			
		}
		
		
		
		public function startTouchSound(){
			var _sound:Sound	 = new Sound();
			var requestStart:URLRequest = new URLRequest("touch.mp3");
			var buff:SoundLoaderContext = new SoundLoaderContext(5000);
 			_sound.load(requestStart,buff);
			_sound.play(10);		
		}
		public function endTouchSound(){
			var _soundEnd:Sound	 = new Sound();
			var requestEnd:URLRequest = new URLRequest("end.mp3");
			var buff:SoundLoaderContext = new SoundLoaderContext(5000);
			_soundEnd.load(requestEnd,buff);
			_soundEnd.play();		
		}
		
		override protected function onRenderTick(event:Event=null):void	{
			papervision3dphysics.engine.integrate(.95);
			camera.lookAt(new DisplayObject3D());
			
			var  count:int = 0;
			for each (var collisioninfo:CollisionInfo in RigidBody(arrRigidBodies[indexShooting]).collisions){
				//				trace("["+count+"]body0.type:"+collisioninfo.objInfo.body0.type);
				//				trace("["+count+"]body0.type:"+collisioninfo.objInfo.body1.type);
				//				trace("");
				
				if(	collisioninfo.objInfo.body0.type=="SPHERE"&&
					collisioninfo.objInfo.body1.type=="SPHERE"&&
					!buttonShooting.enabled
				){
					
					if(RigidBody(arrRigidBodies[indexShooting]).id!=collisioninfo.objInfo.body0.id){
						trace("0_collisioninfo.objInfo.body0.id:"+collisioninfo.objInfo.body0.id);
						if(collisioninfo.objInfo.body0.id==6){
							flagRed1 = true;
							trace("0_flagRed1 = true;");
						}else if(collisioninfo.objInfo.body0.id==7){
							flagRed2 = true;
							trace("0_flagRed2 = true;");
						}else{
							flagOther = true;
							trace("0_flagOther = true;");
						}
					}
					if(RigidBody(arrRigidBodies[indexShooting]).id!=collisioninfo.objInfo.body1.id){
						trace("1_collisioninfo.objInfo.body1.id:"+collisioninfo.objInfo.body1.id);
						if(collisioninfo.objInfo.body1.id==6){
							flagRed1 = true;
							trace("1_flagRed1 = true;");
						}else if(collisioninfo.objInfo.body1.id==7){
							flagRed2 = true;
							trace("1_flagRed2 = true;");
						}else{
							flagOther = true;
							trace("1_flagOther = true;");
						}
						
					}
					
					startTouchSound();
					count++;
				}
				
			}
			
			
			if(	(!RigidBody(arrRigidBodies[0]).isActive)&&
				(!RigidBody(arrRigidBodies[1]).isActive)&&
				(!RigidBody(arrRigidBodies[2]).isActive)&&
				(!RigidBody(arrRigidBodies[3]).isActive)&&
				(!buttonShooting.enabled)
			){
				buttonShooting.enabled = true;
				buttonShooting.label = "Shooting";
				endTouchSound();
				//				if(indexShooting==0){
				//					indexShooting=1;
				//				}else if(indexShooting==1){
				//					indexShooting=0;
				//				}
				trace("flagRed1:"+flagRed1);
				trace("flagRed2:"+flagRed2);
				trace("flagOther:"+flagOther);
				var score:int = 0 ;
				if(flagOther){
					score = -1;
				}else{
					if(flagRed1&&flagRed2){
						score = 1;
					}else if(!flagRed1&&!flagRed2){
						score = -1;
					}
				}
				labeScore.text = int(labeScore.text)+score+"";
				
				
				trace("indexShooting[score]:"+score);
				
			}
			
			super.onRenderTick(event);
		}
		
		
		
		
		
	}
}





